import { Component, OnInit, Input } from '@angular/core';
import { HttpService } from 'src/app/shared/service/http.service';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { EventService } from 'src/app/shared/service/event.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
@Component({
  selector: 'app-compute-report',
  templateUrl: './compute-report.component.html',
  styleUrls: ['./compute-report.component.scss'],
})
export class ComputeReportComponent implements OnInit {
  @Input() tagData: any;
  pageNo: any = 1;
  params: any;
  pageSize: number;
  pageTotal: number;
  showLoadMore = false;
  constructor(
    public httpService: HttpService,
    public appGlobalService: AppGlobalService,
    public eventService: EventService,
    public pageModalService: PageModalService
  ) { }
  ngOnInit() {
    if (this.tagData.headData) {
      this.updateValueColor(this.tagData.headData);
    }
    this.pageTotal = this.tagData.data.pageTotal;
    this.params = this.tagData.data.submit;
    this.pageSize = this.tagData.data.pageSize;
    const listenerName = this.tagData.name + 'AskForList';
    if (this.eventService.eventEmit.listenerCount(listenerName) > 0) {
      this.eventService.eventEmit.removeListener(listenerName);
    }
    this.eventService.eventEmit.on(listenerName, (obj) => {
      this.refreshListData(obj);
    });
  }
  getInterger(value) {
    return parseInt(value, 10);
  }

  refreshListData(param) {
    const that = this;
    if (param === undefined) {
      return;
    }
    this.pageNo = 1;
    that.params.content = param;
    if (param.filter.endDate && param.filter.startDate) {
      that.params.content["endDate"] = param.filter.endDate;
      that.params.content["startDate"] = param.filter.startDate;
    }
    if (that.tagData.key) {
      that.params.id = that.tagData.key;
    }
    that.params.sessionId = localStorage.getItem('sessionId');
    this.httpService.post('application/submit', that.params, res => {
      if (res.success) {
        if (this.tagData.headData) {
          if (this.tagData.headData.length > 0) {
            this.tagData.headData = [];
          } else if (this.tagData.headData.data && this.tagData.headData.data.length > 0) {
            this.tagData.headData.data = [];
          }
        }
        if (this.tagData.data.bodyData) {
          if (this.tagData.data.bodyData.length > 0) {
            this.tagData.data.bodyData = [];
          } else if (this.tagData.data.bodyData.data && this.tagData.data.bodyData.data.length > 0) {
            this.tagData.data.bodyData.data = [];
          }
        }
        if (this.tagData.data.headData && this.tagData.data.headData.data) {
          this.tagData.data.headData.data = [];
          this.tagData.data.headData.title = '';
        }
        if (this.tagData.data.title) {
          this.tagData.data.title = '';
        }

        if (res.data.submit) {
          this.params = res.data.submit; // 为了每次刷新下一页的参数 "oldIndex"
        }
        if (res.data.data && res.data.data.submit) {
          this.params = res.data.data.submit; // 为了每次刷新下一页的参数 "oldIndex"
        }
        if (res.data.data && res.data.data.bodyData) {
          this.tagData.data.bodyData = res.data.data.bodyData;
        }

        if (res.data.data && res.data.data.headData && res.data.data.headData.data) {
          this.tagData.data.headData.data = res.data.data.headData.data;
          this.tagData.data.headData.title = res.data.data.headData.title;
        }

        if (res.data.data && res.data.data.title) {
          this.tagData.data.title = res.data.data.title;
        }

        if (res.data.headData && res.data.headData.data && res.data.headData.data.length > 0) {
          this.tagData.headData = res.data.headData;
        } else if (res.data.headData && res.data.headData.length > 0) {
          this.tagData.headData = res.data.headData;
          this.updateValueColor(this.tagData.headData);
        }

        this.pageTotal = this.tagData.data.pageTotal;

      } else {
        // 请求数据失败
        this.tagData.data.data = [];
        this.appGlobalService.showToast(res.message, 2000, 'middle');
      }
    },
      err => {
        // 请求数据失败
        this.tagData.data.data = [];
        this.appGlobalService.showToast(err.message, 2000, 'middle');
      });
  }

  updateValueColor(data) {
    if (data.length > 0) {
      data.forEach(e => {
        if (e.subValue && e.subValue.indexOf("%") > -1) {
          if (Number(e.subValue.split('%')[0]) > 0) {
            e.class = 0;
          } else if (Number(e.subValue.split('%')[0]) < 0) {
            e.class = 1;
          } else if ((e.subValue.split('%')[0] - 0) === 0) {
            e.subValue = "持平";
          }
        }
      });
    }
  }

  openPage(link) {
    if (link) {
      this.pageModalService.openPage(link);
    }
  }

  loadData(event) {
    this.pageNo = this.pageNo + 1;
    // tslint:disable-next-line: max-line-length
    if (this.tagData.data && this.tagData.data.bodyData && this.tagData.data.bodyData.length !== undefined && this.tagData.data.bodyData.length >= this.tagData.data.pageTotal) {
      event.target.disabled = true;
      return;
    // tslint:disable-next-line: max-line-length
    } else if (this.tagData.data.bodyData && this.tagData.data.bodyData.data && this.tagData.data.bodyData.data.length >= this.tagData.data.pageTotal) {
      event.target.disabled = true;
      return;
    }
    if (this.tagData.data.submit) {
      if (this.tagData.data.submit.content) {
        this.tagData.data.submit.content.pageNo = this.pageNo;
      }
      this.tagData.data.submit.sessionId = localStorage.getItem("sessionId");
    }

    this.httpService.post(
      "application/submit",
      this.tagData.data.submit,
      res => {
        // 请求数据成功
        if (res.success) {
          const resList = res.data.data;
          this.tagData.data.submit = res.data.submit;
          if (this.tagData.data.bodyData && this.tagData.data.bodyData.data) {
            res.data.bodyData.data.map(v => {
              this.tagData.data.bodyData.data.push(v);
            });
          }
          if (this.tagData.data.bodyData && this.tagData.data.bodyData.length > 0) {
            res.data.bodyData.map(v => {
              this.tagData.data.bodyData.push(v);
            });
          }
        } else {
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, "middle");
        }
        event.target.complete();
      },
      err => {

        // 请求数据失败
        this.appGlobalService.showToast(err.message, 2000, "middle");
        event.target.complete();
      }
    );
  }
}
